/*
 * 作者：坚果派
 * 公众号：nutpi
 * 电话：17752170152
 * 官网：https://www.nutpi.net/
 */

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../providers/address_provider.dart';
import '../../models/address_model.dart';

class WorldCityTab extends StatefulWidget {
  const WorldCityTab({Key? key}) : super(key: key);

  @override
  State<WorldCityTab> createState() => _WorldCityTabState();
}

class _WorldCityTabState extends State<WorldCityTab> {
  @override
  void initState() {
    super.initState();
    // 页面初始化时加载世界国家城市数据
    Future.microtask(() {
      Provider.of<AddressProvider>(context, listen: false).fetchCountries();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('世界城市列表'),
        centerTitle: true,
      ),
      body: Consumer<AddressProvider>(
        builder: (context, provider, child) {
          if (provider.loadingCountries) {
            return const Center(child: CircularProgressIndicator());
          }

          if (provider.countryError.isNotEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '加载失败: ${provider.countryError}',
                    style: const TextStyle(color: Colors.red),
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: () {
                      provider.fetchCountries();
                    },
                    child: const Text('重试'),
                  ),
                ],
              ),
            );
          }

          if (provider.countries.isEmpty) {
            return const Center(child: Text('暂无数据'));
          }

          return ListView.builder(
            itemCount: provider.countries.length,
            itemBuilder: (context, index) {
              final country = provider.countries[index];
              return _buildCountryItem(country);
            },
          );
        },
      ),
    );
  }

  Widget _buildCountryItem(Country country) {
    return ExpansionTile(
      title: Text(
        country.name,
        style: const TextStyle(fontWeight: FontWeight.bold),
      ),
      subtitle: Text('编码: ${country.code}'),
      children: country.clist.map((province) => _buildProvinceItem(province)).toList(),
    );
  }

  Widget _buildProvinceItem(Province province) {
    return Padding(
      padding: const EdgeInsets.only(left: 16.0),
      child: ExpansionTile(
        title: Text(province.name),
        subtitle: Text('编码: ${province.code}'),
        children: province.pchilds.map((city) => _buildCityItem(city)).toList(),
      ),
    );
  }

  Widget _buildCityItem(City city) {
    return Padding(
      padding: const EdgeInsets.only(left: 32.0),
      child: ExpansionTile(
        title: Text(city.name),
        subtitle: Text('编码: ${city.code}'),
        children: city.cchilds.map((district) => _buildDistrictItem(district)).toList(),
      ),
    );
  }

  Widget _buildDistrictItem(District district) {
    return Padding(
      padding: const EdgeInsets.only(left: 48.0),
      child: ListTile(
        title: Text(district.name),
        subtitle: Text('编码: ${district.code}'),
        dense: true,
      ),
    );
  }
}